//
//  UIView+Extension.swift
//  YangFoundation
//
//  Created by yuyang on 2021/12/21.
//

import UIKit

// MARK: - UIView Normal Extension
public extension UIView {

    var x: CGFloat {
        get {
            return frame.origin.x
        }

        set(newValue) {
            var tmpFrame: CGRect = frame
            tmpFrame.origin.x     = newValue
            frame                 = tmpFrame
        }
    }

    var y: CGFloat {
        get {
            return frame.origin.y
        }

        set(newValue) {
            var tmpFrame: CGRect = frame
            tmpFrame.origin.y     = newValue
            frame                 = tmpFrame
        }
    }

    var height: CGFloat {
        get {
            return frame.size.height
        }

        set(newValue) {
            var tmpFrame: CGRect = frame
            tmpFrame.size.height  = newValue
            frame                 = tmpFrame
        }
    }

    var width: CGFloat {
        get {
            return frame.size.width
        }

        set(newValue) {
            var tmpFrame: CGRect = frame
            tmpFrame.size.width   = newValue
            frame                 = tmpFrame
        }
    }

    var left: CGFloat {
        get {
            return x
        }

        set(newValue) {
            x = newValue
        }
    }

    var right: CGFloat {
        get {
            return x + width
        }

        set(newValue) {
            x = newValue - width
        }
    }

    var top: CGFloat {
        get {
            return y
        }

        set(newValue) {
            y = newValue
        }
    }

    var bottom: CGFloat {
        get {
            return y + height
        }

        set(newValue) {
            y = newValue - height
        }
    }

    var centerX: CGFloat {
        get {
            return center.x
        }

        set(newValue) {
            center = CGPoint(x: newValue, y: center.y)
        }
    }

    var centerY: CGFloat {
        get {
            return center.y
        }

        set(newValue) {
            center = CGPoint(x: center.x, y: newValue)
        }
    }

    var middleX: CGFloat {
        return width / 2
    }

    var middleY: CGFloat {
        return height / 2
    }

    var middlePoint: CGPoint {
        return CGPoint(x: middleX, y: middleY)
    }

    @discardableResult
    func setBackGroundColor(_ color: UIColor) -> UIView {
        backgroundColor = color
        return self
    }

    @discardableResult
    func setTag(_ viewTag: Int) -> UIView {
        tag = viewTag
        return self
    }

    /// 添加多个子视图
    func addSubViews(_ views: UIView...) {
        for view in views {
            addSubview(view)
        }
    }

    /// 获取当前vc, 支持 UINavigationController, UITabBarController, presentedViewController
    func currentViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let nav = base as? UINavigationController {
            return currentViewController(base: nav.visibleViewController)
        }
        if let tab = base as? UITabBarController {
            return currentViewController(base: tab.selectedViewController)
        }
        if let presented = base?.presentedViewController {
            return currentViewController(base: presented)
        }
        return base
    }
}

// MARK: - UIView View Extension
public extension UIView {
    /// 任意圆角
    /// - Warming：使用前一定确保view的约束或size被正确设置
    /// - Parameter radius: 圆角大小
    /// - Parameter corners: 圆角方向
    func freeCorner(_ radius: CGFloat, corners: UIRectCorner..., fillColor: UIColor = .clear, borderWidth: Float = 1) {
        layoutIfNeeded()
        let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: .init(corners), cornerRadii: CGSize(width: radius, height: radius))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = bounds
        if fillColor != .clear {
            maskLayer.strokeColor = fillColor.cgColor
            maskLayer.lineWidth = CGFloat(borderWidth)
        }
        maskLayer.path = maskPath.cgPath
        
        layer.mask = maskLayer
    }
}
